<template>
  <div class="app-container">
    <el-row :gutter="20">
      
      <!--账户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="账户名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入账户名称查找"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="账户编号" prop="serialNo">
            <el-input
              v-model="queryParams.serialNo"
              placeholder="请输入账户编号查找"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
              <el-input
                v-model="queryParams.remark"
                placeholder="请输入备注"
                clearable
                size="small"
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['basic:account:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['basic:account:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['basic:account:remove']"
            >删除</el-button>
          </el-col>
          
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column type="index" width="50" align="center" /> 
          <el-table-column label="账户名称" align="center" key="name" prop="name" v-if="columns[0].visible" :show-overflow-tooltip="true" />
          <el-table-column label="账户编号" align="center" key="serialNo" prop="serialNo" v-if="columns[1].visible" />
          <el-table-column label="期初金额" align="center" key="initialAmount" prop="initialAmount" v-if="columns[2].visible" />
          <el-table-column label="当前余额" align="center" key="currentAmount" prop="currentAmount" v-if="columns[3].visible" />
          <el-table-column label="备注" align="center" key="remark" prop="remark" v-if="columns[4].visible" />
          <el-table-column label="是否默认" align="center" key="isDefault" prop="isDefault"  v-if="columns[5].visible" >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.isDefault==1">是</el-tag>
              <el-tag v-if="scope.row.isDefault==0">否</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['basic:account:edit']"
              >修改</el-button>   
              <el-popconfirm
                title="确定设为默认吗？"
                @confirm="handleDefault(scope.row.id)"	
                 v-hasPermi="['basic:account:default']"
              >
                <el-button
                  slot="reference"     
                  size="mini"
                  type="text"
                  icon="el-icon-edit"           
                >设为默认</el-button>  
                <el-button >删除</el-button>
              </el-popconfirm>
              

            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.limit"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入账户名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户编号" prop="serialNo">
              <el-input v-model="form.serialNo" placeholder="请输入账户编号" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="期初金额" prop="initialAmount">
              <el-input v-model="form.initialAmount" placeholder="请输入期初金额" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="当前余额" prop="currentAmount">
              <el-input v-model="form.currentAmount" placeholder="请输入当前余额" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否默认" prop="isDefault">
               <el-radio v-model="form.isDefault" :label="1">是</el-radio>
               <el-radio v-model="form.isDefault" :label="0">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {listAccount,getAccount, delAccount, addAccount, updateAccount,updateIsDefault } from "@/api/basic/account";

export default {
  name: "index",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账户表格数据
      userList: null,
      // 弹出层标题
      title: "",
     
      // 是否显示弹出层
      open: false,
 
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },

      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        name: undefined,
        serialNo: undefined,
        remark: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `账户名称`, visible: true },
        { key: 1, label: `账户编号`, visible: true },
        { key: 2, label: `期初金额`, visible: true },
        { key: 3, label: `当前余额`, visible: true },
        { key: 4, label: `备注`, visible: true },
        { key: 5, label: `是否默认`, visible: true }
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "账户名称不能为空", trigger: "blur" }
        ],
        serialNo: [
          { required: true, message: "账户编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询账户列表 */
    getList() {
      this.loading = true;
      listAccount(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.data.list;
          this.total = response.data.totalCount;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        serialNo: undefined,
        initialAmount: undefined,
        currentAmount: undefined,
        remark: undefined,
        principal: undefined,
        isDefault: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
   
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改账户";
      });
      
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除选择的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAccount(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
  
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAccount(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccount(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDefault(id){
      updateIsDefault(id).then(response => {
        this.msgSuccess("修改成功");
        this.getList();
      })
    }
   
    
  }
}
</script>